package org.tyut.xyl.game;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.liugang.tugofwar.R;

import org.tyut.xyl.game.util.GsonUtil;
import org.tyut.xyl.game.util.Information;
import org.tyut.xyl.game.util.OkHttpUtil;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class JoinRoomActivity extends AppCompatActivity {
    private EditText joinroom_roomid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_room);

        joinroom_roomid = (EditText) findViewById(R.id.join_room_roomid);
        final RadioGroup radio_group = findViewById(R.id.radio_group);
        MainActivity.roommaster = "0";
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton checkbutton = (RadioButton) findViewById(radio_group.getCheckedRadioButtonId());
                String team = checkbutton.getText().toString();
                if (team.equals("红队"))
                    MainActivity.team = "red";
                else
                    MainActivity.team = "blue";
            }
        });


    }

    public void onClickToJoinTeam(View view) {
        MainActivity.roomid = joinroom_roomid.getText().toString();
        FormBody formBody = new FormBody.Builder()
                .add("roomid", MainActivity.roomid)
                .add("username", MainActivity.username)
                .add("team", MainActivity.team)
                .build();
        Request request = new Request.Builder().url(Information.URL + "joinroom")
                .post(formBody).build();
        Call call = OkHttpUtil.getOkHttpClient().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Map<String, String> map = GsonUtil.getGsonBuilder().create().fromJson(response.body().string(), Map.class);
                if (map.get("state").equals("success")) {
                    Intent intent = new Intent(JoinRoomActivity.this, RoomActivity.class);
                    startActivity(intent);
                } else {
                    TextView tip = findViewById(R.id.join_room_tip);
                    tip.setText("房间不存在！");
                }
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                TextView tip = findViewById(R.id.join_room_tip);
                tip.setText("房间不存在！");
            }
        });
    }

    public void onClikcToChangeTeam(RadioGroup group, int checkedId) {
        RadioGroup radio_group = findViewById(R.id.radio_group);
        RadioButton checkbutton = (RadioButton) findViewById(radio_group.getCheckedRadioButtonId());
        String team = checkbutton.getText().toString();
        if (team.equals("红队"))
            MainActivity.team = "red";
        else
            MainActivity.team = "blue";
    }
}
